package com.jboost.modules.app.constant;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.dto.JdyReq;
import com.jboost.modules.app.dto.XMUserReqDto;
import com.jboost.modules.app.dto.resp.GcKcResp;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpHeaders;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.jboost.modules.app.util.JdyFileUtils.UUIDS;

public interface Strategy<T, V> {

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    String delete_api = "https://api.jiandaoyun.com/api/v5/app/entry/data/delete";
    String add_api = "https://api.jiandaoyun.com/api/v5/app/entry/data/create";
    String query_api = "https://api.jiandaoyun.com/api/v5/app/entry/data/get";
    String update_api = "https://api.jiandaoyun.com/api/v5/app/entry/data/update";
    String query_all_api = "https://api.jiandaoyun.com/api/v5/app/entry/data/list";
    default BaseResult<V> invokeStrategy(T t) throws Exception {
        return null;
    }

   default BaseResult<Boolean> invokeStrategy() throws Exception {
        return null;
    }

   default Map<String, Object> getParaMap(Object o) {
        Map map = Maps.newHashMap();
        map.put("value",o);
        return map;
    }


    default HttpHeaders getJdyHeaders(){
        return null;
    }

    default String jdyCreate(Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("data", data);
        String post = HttpRestUtils.post(add_api, param, getJdyHeaders());
        return post;
    }

    default String jdyCreate1(Object data) throws IOException {
        HashMap<String, Object> param = getParam1();
        param.put("data", data);
        if (Objects.nonNull(UUIDS.get())){
            param.put("transaction_id", UUIDS.get());
        }
        String post = HttpRestUtils.post(add_api, param, getJdyHeaders());
        return post;
    }

    default String jdyCreate2(Object data) throws IOException {
        HashMap<String, Object> param = getParam2();
        param.put("data", data);
        if (Objects.nonNull(UUIDS.get())){
            param.put("transaction_id", UUIDS.get());
        }
        String post = HttpRestUtils.post(add_api, param, getJdyHeaders());
        return post;
    }


    default String jdyCreateFlow(Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("data", data);
        param.put("is_start_workflow",true);
        param.put("is_start_trigger",true);
        String post = HttpRestUtils.post(add_api, param, getJdyHeaders());
        return post;
    }

    default String jdyDelete(String data_id) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("data_id", data_id);
        String post = HttpRestUtils.post(delete_api , param, getJdyHeaders());
        return post;
    }

    default String jdyDelete1(String data_id) throws IOException {
        HashMap<String, Object> param = getParam1();
        param.put("data_id", data_id);
        String post = HttpRestUtils.post(delete_api , param, getJdyHeaders());
        return post;
    }

    default String jdyDelete2(String data_id) throws IOException {
        HashMap<String, Object> param = getParam2();
        param.put("data_id", data_id);
        String post = HttpRestUtils.post(delete_api , param, getJdyHeaders());
        return post;
    }

    default String jdyUpdate(String data_id,Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("data_id", data_id);
        param.put("data", data);
        String post = HttpRestUtils.post(update_api , param, getJdyHeaders());
        return post;
    }

    default String jdyUpdate1(String data_id,Object data) throws IOException {
        HashMap<String, Object> param = getParam1();
        param.put("data_id", data_id);
        param.put("data", data);
        String post = HttpRestUtils.post(update_api , param, getJdyHeaders());
        return post;
    }

    default String jdyQueryAll(Map para) throws IOException {
        HashMap<String, Object> param = getParam();
        param.putAll(para);
        String post = HttpRestUtils.post(query_all_api , param, getJdyHeaders());
        return post;
    }


    default String jdyQueryAll(JdyReq para) throws IOException {
        Gson gson = new Gson();
        String json = gson.toJson(para);
        Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
        String post = HttpRestUtils.post(query_all_api , map, getJdyHeaders());
        return post;
    }



    default String getAppId(){
        return null;
    }


    default String getFormId(){
        return null;
    }

    default String getFormId1(){
        return null;
    }

    default String getFormId2(){
        return null;
    }
    default HashMap<String, Object> getParam(){
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("app_id",getAppId());
        paramMap.put("entry_id",getFormId());
        return paramMap;
    };

    default HashMap<String, Object> getParam1(){
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("app_id",getAppId());
        paramMap.put("entry_id",getFormId1());
        return paramMap;
    };

    default HashMap<String, Object> getParam2(){
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("app_id",getAppId());
        paramMap.put("entry_id",getFormId2());
        return paramMap;
    };

    default List list(String api, HttpHeaders jdyHeaders,Class tclass) throws IOException, NoSuchFieldException, IllegalAccessException {
        List rows = Lists.newArrayList();
        int index = 1;
        Map<String,Object> param = Maps.newHashMap();
        param.put("page",index);
        param.put("pageSize",20);
        String post = HttpRestUtils.post(api, param, jdyHeaders);
        Object gcKcResp = JSONUtil.fromJson(post, tclass);
        Field data = gcKcResp.getClass().getDeclaredField("data");
        rows.addAll((Collection) data.get(gcKcResp));
        while (!CollectionUtils.isEmpty((Collection<?>) data.get(gcKcResp))){
            ++index;
            param.put("page",index);
            post = HttpRestUtils.post(api, param, jdyHeaders);
            gcKcResp = JSONUtil.fromJson(post, tclass);
            rows.addAll((Collection) data.get(gcKcResp));

        }
        return rows;
    }
}
